package ru.mcfr.oxygen.framework.operations.table;

import ro.sync.ecss.extensions.api.node.AuthorElement;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class TableInfo {

    //private final String title;
    private final String tableTagName = "таблица";
    private final String columnGroupTagName = "группировка_колонок";
    private final String columnTagName = "колонка";
    private final String rowTagName = "строка";
    private final String cellTagName = "ячейка";
    private final String rowTypeAttrName = "тип_строки";
    private final String headerRowTypeAttrValue = "шапка";
    private final String footerRowTypeAttrValue = "подвал";
    private final String rowspanAttrName = "rowspan";
    private final String colspanAttrName = "colspan";

    private final String[] typesOfBorder = "все,левая,правая,нижняя,верхняя".split(",");

    private AuthorElement[][] cellsMatrix;

    private int rowsNumber = 0;

    private int columnsNumber = 0;

    private int headerRowsCount = 0;

    private int footerRowsCount = 0;

    private String typeOfBorder;

    //private final Color tableBgColor;

    //for filling cells when paste new table
    private Map<String, String> CellContent;

    private String currentCellContent = "";

    private String fuck = "";

    public String[] getTypesOfBorder() {
        return typesOfBorder;
    }

    public String getTypeOfBorder() {
        return typeOfBorder;
    }

    public void setTypeOfBorder(String typeOfBorder) {
        this.typeOfBorder = typeOfBorder;
    }

    public String getFuck() {
        return fuck;
    }

    public void setFuck(String fuck) {
        this.fuck = fuck;
    }

    public TableInfo(int rowsNumber, int columnsNumber) {
        this(rowsNumber, columnsNumber, 0, 0);
    }

    public TableInfo(int rowsNumber, int columnsNumber, int rowsForHeader, int rowsForFooter) {
        this.CellContent = new HashMap<String, String>();
        this.CellContent.put("", "");
        this.rowsNumber = rowsNumber;
        this.columnsNumber = columnsNumber;
        this.headerRowsCount = rowsForHeader;
        this.footerRowsCount = rowsForFooter;
    }

    public TableInfo(AuthorElement tableElement) {
        if (tableTagName.equals(tableElement.getLocalName())) {
            this.columnsNumber = tableElement.getElementsByLocalName(columnTagName).length;

            AuthorElement[] rows = tableElement.getElementsByLocalName(rowTagName);
            this.rowsNumber = rows.length;

            cellsMatrix = new AuthorElement[this.columnsNumber][this.rowsNumber];

            Map<AuthorElement, Integer[]> rowspans = new HashMap<AuthorElement, Integer[]>();
            Map<AuthorElement, Integer[]> colspans = new HashMap<AuthorElement, Integer[]>();
            int currentRow = 0;
            int currentCol = 0;

            for (AuthorElement row : rows) {
                String rowType = row.getAttribute(rowTypeAttrName).getValue();
                if (headerRowTypeAttrValue.equals(rowType))
                    this.headerRowsCount++;
                else if (footerRowTypeAttrValue.equals(rowType))
                    this.footerRowsCount++;

                AuthorElement[] cells = row.getElementsByLocalName(cellTagName);
                for (AuthorElement cell : cells) {
                    String rowspanStringValue = "";
                    try {
                        rowspanStringValue = cell.getAttribute(rowspanAttrName).getValue();
                        Integer[] spanRange = new Integer[2];
                        spanRange[0] = Integer.valueOf(currentRow);
                        spanRange[1] = Integer.valueOf(currentRow) + Integer.getInteger(rowspanStringValue);
                        rowspans.put(cell, spanRange);
                    } catch (Exception e) {
                    }

                    String colspanStringValue = "";
                    try {
                        colspanStringValue = cell.getAttribute(colspanAttrName).getValue();
                        Integer[] spanRange = new Integer[2];
                        spanRange[0] = Integer.valueOf(currentCol);
                        spanRange[1] = Integer.valueOf(currentCol) + Integer.getInteger(colspanStringValue);
                        colspans.put(cell, spanRange);
                    } catch (Exception e) {
                    }

                    cellsMatrix[currentRow][currentCol] = cell;

                    currentCol++;
                }

                currentRow++;
            }
        }
    }

//	public String getTitle() {
//		return title;
//	}

    public int getHeaderRowsCount() {
        return headerRowsCount;
    }

    public int getFooterRowsCount() {
        return footerRowsCount;
    }

    public String getFuckingGet(String key) {
        return CellContent.get(key);
    }

    public String getCurrentCellContent() {
        return currentCellContent;
    }

    public void setCurrentCellContent(String text) {
        currentCellContent = CellContent.get(text);
    }

    public void addCellContent(String name, String xmlString) {
        CellContent.put(name, xmlString);
    }

    public Set<String> getCellContentSet() {
        return CellContent.keySet();
    }

    public int getRowsNumber() {
        return rowsNumber;
    }

    public int getColumnsNumber() {
        return columnsNumber;
    }
}


